package com.jed.xxh.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.util.Base64;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

/**
 * 项目名：stewardAPP<br/>
 * 包名：  com.jed.xxh.utils<br/>
 * 文件名：UtilTools<br/>
 * 作者：徐晓辉<br/>
 * 创建日期：2018/11/24 12:01<br/>
 * 描述：工具类<br/>
 */
public class UtilTools {

    /**
     * 设置字体
     *
     * @param mContext
     * @param textView
     */
    public static void setFont(Context mContext, TextView textView) {
        Typeface fromAsset = Typeface.createFromAsset(mContext.getAssets(), "fonts/FONT.TTF");
        textView.setTypeface(fromAsset);
    }

    /**
     * 保存图片到Share中
     */
    public static void putImageToShare(Context context, ImageView ImageView) {

        BitmapDrawable bitmapDrawable = (BitmapDrawable) ImageView.getDrawable();
        Bitmap bitmap = bitmapDrawable.getBitmap();
        //第一步：将bitmap压缩成为字节输出流
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        boolean compress = bitmap.compress(Bitmap.CompressFormat.PNG, 80, byteArrayOutputStream);
        //第二步：利用Base64将字节数组输出流转为String
        byte[] bytes = byteArrayOutputStream.toByteArray();
        String imageStr = new String(Base64.encodeToString(bytes, Base64.DEFAULT));
        //第三步：将String 保存到ShareUtil中
        ShareUtil.putString(StaticClass.USER_AVATAR, imageStr);
    }

    /**
     * Share中获取图片
     */
    public static void getImageToShare(Context context, ImageView ImageView, String imageKay) {
        if (!imageKay.equals("") && imageKay != null) {
            byte[] decode = Base64.decode(imageKay, Base64.DEFAULT);
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(decode);
            Bitmap bitmap = BitmapFactory.decodeStream(byteArrayInputStream);
            ImageView.setImageBitmap(bitmap);
        }
    }
}
